package com.hmkj.admin.controller.simulation;

import com.hmkj.admin.controller.BaseController;
import com.hmkj.admin.controller.GenericController;
import com.hmkj.admin.utils.AdminSessionHelper;
import com.hmkj.common.entity.ResultEntity;
import com.hmkj.common.entity.dtgrid.DtGrid;
import com.hmkj.common.utils.StringUtils;
import com.hmkj.core.eolinker.endpoints.QuotationEndpoint;
import com.hmkj.core.eolinker.models.ModelRealTime;
import com.hmkj.core.exception.BussinessException;
import com.hmkj.core.po.index.EquityRecommend;
import com.hmkj.core.po.sysNotice.SystemNotice;
import com.hmkj.core.service.index.EquityRecommendService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
@RequestMapping(BaseController.BASE_URI)
public class HotEquityJsonController extends GenericController<Integer, EquityRecommend> {
    @Resource
    private EquityRecommendService equityRecommendService;

    /**
     * 热门股票列表json数据
     *
     * @param dtGridPager
     * @return
     */
    @ResponseBody
    @RequestMapping("hotStock/json/list")
    public DtGrid userListJson(String dtGridPager) throws Exception {
        return equityRecommendService.getDtGridList(dtGridPager, EquityRecommend.class);
    }
    /**
     * 新增热门股票
     *
     * @param
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("hotStock/json/save")
    @RequiresPermissions("sys:hotStock:save")
    public ResultEntity addJson(String equityCode, String equityName) throws Exception {
        checkModel(equityCode, equityName);
        if (equityRecommendService.selectCount(new EquityRecommend(t -> {
            t.setEquityCode(equityCode);
            t.setDelFlag(EquityRecommend.DELFLAG.NORMAL.code);
        }))>0 || equityRecommendService.selectCount(new EquityRecommend(t -> {
            t.setEquityName(equityName);
            t.setDelFlag(EquityRecommend.DELFLAG.NORMAL.code);
        }))>0) {
            throw new BussinessException("股票名称或股票代码已存在");
        }
        //获取股票信息
        ModelRealTime modelRealTime = QuotationEndpoint.realtime(equityCode);
        if(null == modelRealTime){
            throw new BussinessException("股票不存在");
        }
        equityRecommendService.insert(new EquityRecommend(t -> {
            t.setStatus(EquityRecommend.STATUS.T1.code);
            t.setCreator(AdminSessionHelper.getCurrAdmin().getName());
            t.setEquityCode(equityCode);
            t.setEquityName(equityName);
        }));
        return ok();
    }

    /**
     * 编辑热门股票
     *
     * @param
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("hotStock/json/edit")
    @RequiresPermissions("sys:hotStock:edit")
    public ResultEntity editJson(Integer id,String equityCode, String equityName) throws Exception {
        checkModel(equityCode, equityName);
        EquityRecommend equityRecommend=checkPrimaryKey(id);
        if (!equityRecommend.getEquityCode().equals(equityCode) || !equityRecommend.getEquityName().equals(equityName)){
            if (equityRecommendService.selectCount(new EquityRecommend(t -> {
                t.setEquityCode(equityCode);
                t.setDelFlag(EquityRecommend.DELFLAG.NORMAL.code);
            }))>0 || equityRecommendService.selectCount(new EquityRecommend(t -> {
                t.setEquityName(equityName);
                t.setDelFlag(EquityRecommend.DELFLAG.NORMAL.code);
            }))>0) {
                throw new BussinessException("股票名称或股票代码已存在");
            }
            //获取股票信息
            ModelRealTime modelRealTime = QuotationEndpoint.realtime(equityCode);
            if(null == modelRealTime){
                throw new BussinessException("股票不存在");
            }
            equityRecommendService.updateByPrimaryKeySelective(new EquityRecommend(t -> {
                t.setId(id);
                t.setStatus(EquityRecommend.STATUS.T1.code);
                t.setCreator(AdminSessionHelper.getCurrAdmin().getName());
                t.setEquityCode(equityCode);
                t.setEquityName(equityName);
            }));
        }
        return ok();
    }

    /**
     * 发布
     *
     * @param id
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "hotStock/json/publish")
    @RequiresPermissions("sys:hotStock:publish")
    public ResultEntity publish(Integer id) throws Exception {
        if (id == null) {
            throw new BussinessException("参数有误");
        }
        EquityRecommend equityRecommend = equityRecommendService.selectOne(new EquityRecommend(a -> {
            a.setId(id);
            a.setDelFlag(SystemNotice.DELFLAG.NORMAL.code);
        }));
        if (null == equityRecommend) {
            throw new BussinessException("您要发布的内容不存在或已被删除");
        } else if (equityRecommend.getStatus() == EquityRecommend.STATUS.T2.code) {
            throw new BussinessException("当前状态下不能操作");
        }
        equityRecommendService.updateByPrimaryKeySelective(new EquityRecommend(a -> {
            a.setId(equityRecommend.getId());
            a.setStatus(EquityRecommend.STATUS.T2.code);
        }));
        return ok();
    }

    /**
     * 撤回
     *
     * @param id
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "hotStock/json/back")
    @RequiresPermissions("sys:hotStock:back")
    public ResultEntity back(Integer id) throws Exception {
        if (id == null) {
            throw new BussinessException("参数有误");
        }
        EquityRecommend equityRecommend = equityRecommendService.selectOne(new EquityRecommend(a -> {
            a.setId(id);
            a.setDelFlag(EquityRecommend.DELFLAG.NORMAL.code);
        }));
        if (null == equityRecommend) {
            throw new BussinessException("您要撤回的内容不存在或已被删除");
        } else if (equityRecommend.getStatus() != EquityRecommend.STATUS.T2.code) {
            throw new BussinessException("当前状态下不能操作");
        }
        equityRecommendService.updateByPrimaryKeySelective(new EquityRecommend(a -> {
            a.setId(equityRecommend.getId());
            a.setStatus(EquityRecommend.STATUS.T1.code);
        }));
        return ok();
    }

    /**
     * 删除
     *
     * @param id
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value = "hotStock/json/del")
    @RequiresPermissions("sys:hotStock:del")
    public ResultEntity del(Integer id) throws Exception {
        if (id == null) {
            throw new BussinessException("参数有误");
        }
        EquityRecommend equityRecommend = equityRecommendService.selectOne(new EquityRecommend(a -> {
            a.setId(id);
            a.setDelFlag(SystemNotice.DELFLAG.NORMAL.code);
        }));
        if (null == equityRecommend) {
            throw new BussinessException("您要删除的内容不存在或已被删除");
        } else if (equityRecommend.getStatus() == EquityRecommend.STATUS.T2.code) {
            throw new BussinessException("当前状态下不能操作");
        }
        equityRecommendService.updateByPrimaryKeySelective(new EquityRecommend(a -> {
            a.setId(equityRecommend.getId());
            a.setDelFlag(EquityRecommend.DELFLAG.DELETED.code);
        }));
        return ok();
    }

    private void checkModel(String code, String name) {
        if (StringUtils.isBlank(code)) {
            throw new BussinessException("股票代码不能为空");
        } else if (code.length() > 30) {
            throw new BussinessException("股票代码不能超过30位");
        } else if (StringUtils.isBlank(name)) {
            throw new BussinessException("股票名称不能为空");
        } else if (name.length() > 30) {
            throw new BussinessException("股票名称不能超过30位");
        }
    }

    private EquityRecommend checkPrimaryKey(Integer id) throws Exception {
        if (null == id) {
            throw new BussinessException("参数有误");
        } else {
            EquityRecommend equityRecommend = equityRecommendService.selectOne(new EquityRecommend(t -> {
                t.setId(id);
                t.setDelFlag(EquityRecommend.DELFLAG.NORMAL.code);
            }));
            if (null != equityRecommend) {
                return equityRecommend;
            } else {
                throw new BussinessException("参数有误");
            }
        }
    }
}
